package br.com.sovi.gwt.client.component.mainmenu;

import br.com.sovi.gwt.client.ContatoCentralPanel;
import br.com.sovi.gwt.client.ImagesImpl;
import br.com.sovi.gwt.client.MenuLateralPanel;
import br.com.sovi.gwt.client.component.centerpanel.CenterPanel;
import br.com.sovi.gwt.client.controleunitario.ControleUnitarioCentralPanel;
import br.com.sovi.gwt.client.controleunitario.ControleUnitarioMainMenuPanel;
import br.com.sovi.gwt.client.inicio.InicioCentralPanel;
import br.com.sovi.gwt.client.pesquisa.PesquisaCentralPanel;
import br.com.sovi.gwt.client.pesquisa.PesquisaMenuPanel;
import br.com.sovi.gwt.client.rastreamento.RastreamentoCarroTreePanel;
import br.com.sovi.gwt.client.rastreamento.RastreamentoPortal;

import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.event.ButtonListener;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Label;

public class MainMenu extends Toolbar {

	private final MenuLateralPanel menuLateralPanel;

	private final CenterPanel centerPanel;

	public MainMenu(MenuLateralPanel menuLateralPanel, CenterPanel centerPanel) {
		this.menuLateralPanel = menuLateralPanel;
		this.centerPanel = centerPanel;

		InicioButtonListener inicioButtonListener = new InicioButtonListener();
		ToolbarButton inicioButton = createButton("Início", inicioButtonListener);
		this.addButton(inicioButton);
		this.addSeparator();
		this.addButton(createButton("Rastreamento", new RastreamentoButtonListener()));
		this.addSeparator();
		this.addButton(createButton("Controle Unitário", new ControleUnitarioButtonListener()));
		this.addSeparator();
		this.addButton(createButton("Pesquisas", new PesquisaButtonListener()));
		this.addSeparator();
		this.addButton(createButton("Contato", new ContatoButtonListener()));

		// iniciar a pagina
		inicioButton.toggle(true);
		inicioButtonListener.onToggle(inicioButton, true);
	}

	private class InicioButtonListener extends ButtonListenerAdapter {
		@Override
		public void onToggle(Button button, boolean pressed) {
			if (pressed) {
				centerPanel.setTitle(null);
				centerPanel.setContent(new InicioCentralPanel());

				Label label = new Label();

				StringBuilder sb = new StringBuilder();
				sb.append("<table><tr>");
				sb
						.append("<td><div style='font-size: 12px; font-weight: bold; font-family: Arial'>Escolha uma das opções do menu acima.</div></td>");
				sb.append("<td>").append(ImagesImpl.get().up().getHTML()).append("</td>");
				sb.append("</tr></table>");

				label.setHtml(sb.toString());

				menuLateralPanel.setContent(label);
				menuLateralPanel.collapse(true);
			}
		}
	}

	private class RastreamentoButtonListener extends ButtonListenerAdapter {

		private RastreamentoPortal rastreamentoPortal;

		private RastreamentoCarroTreePanel rastreamentoCarroTreePanel;

		@Override
		public void onToggle(Button button, boolean pressed) {
			if (pressed) {
				centerPanel.setTitle("Rastreamento");
				rastreamentoPortal = new RastreamentoPortal();
				centerPanel.setContent(rastreamentoPortal);

				rastreamentoCarroTreePanel = new RastreamentoCarroTreePanel();
				menuLateralPanel.setContent(rastreamentoCarroTreePanel);
				rastreamentoCarroTreePanel.setCarroCheckChangeListener(rastreamentoPortal);
				menuLateralPanel.expand(true);
			}
		}
	}

	private class ControleUnitarioButtonListener extends ButtonListenerAdapter {
		@Override
		public void onToggle(Button button, boolean pressed) {
			if (pressed) {
				centerPanel.setTitle("Controle Unitário");
				final ControleUnitarioCentralPanel panel = new ControleUnitarioCentralPanel();
				centerPanel.setContent(panel);

				menuLateralPanel.setContent(new ControleUnitarioMainMenuPanel(panel));
				menuLateralPanel.expand(true);
			}
		}
	}

	private class PesquisaButtonListener extends ButtonListenerAdapter {
		@Override
		public void onToggle(Button button, boolean pressed) {
			if (pressed) {
				centerPanel.setTitle("Pesquisas");
				PesquisaCentralPanel panel = new PesquisaCentralPanel();
				centerPanel.setContent(panel);
				
				menuLateralPanel.setContent(new PesquisaMenuPanel(panel));
				menuLateralPanel.expand(true);
			}
		}
	}

	private class ContatoButtonListener extends ButtonListenerAdapter {
		@Override
		public void onToggle(Button button, boolean pressed) {
			if (pressed) {
				centerPanel.setTitle(null);
				centerPanel.setContent(new ContatoCentralPanel());

				Label label = new Label();

				StringBuilder sb = new StringBuilder();
				sb.append("<table><tr>");
				sb
						.append("<td><div style='font-size: 12px; font-weight: bold; font-family: Arial'>Escolha uma das opções do menu acima.</div></td>");
				sb.append("<td>").append(ImagesImpl.get().up().getHTML()).append("</td>");
				sb.append("</tr></table>");

				label.setHtml(sb.toString());

				menuLateralPanel.setContent(label);
				menuLateralPanel.collapse(true);
			}
		}
	}
	
	private ToolbarButton createButton(String string, ButtonListener buttonListener) {
		return createButton(string, false, buttonListener);
	}

	private ToolbarButton createButton(String text, boolean toggle, ButtonListener buttonListener) {
		ToolbarButton toolbarButton = new ToolbarButton(text);
		toolbarButton.setToggleGroup("menu");
		toolbarButton.setEnableToggle(true);
		toolbarButton.toggle(toggle);
		if (buttonListener != null) {
			toolbarButton.addListener(buttonListener);
		}
		return toolbarButton;
	}

}
